<template>
	<view class="progress-page">
		<!-- 订单信息 -->
		<view class="order-card" v-if="orderInfo">
			<view class="card-title">订单信息</view>
			<view class="order-info">
				<text class="order-title">{{ orderInfo.title }}</text>
				<view class="info-row">
					<text class="label">预算：</text>
					<text class="value budget">￥{{ orderInfo.budget }}</text>
				</view>
				<view class="info-row">
					<text class="label">截止时间：</text>
					<text class="value">{{ formatTime(orderInfo.deadline) }}</text>
				</view>
			</view>
		</view>
		
		<!-- 进度更新 -->
		<view class="progress-section">
			<view class="section-title">更新进度</view>
			
			<view class="progress-form">
				<view class="form-item">
					<text class="form-label">完成进度</text>
					<slider 
						:value="progressValue" 
						@change="onProgressChange"
						min="0" 
						max="100" 
						step="5"
						activeColor="#007AFF"
						backgroundColor="#e9ecef"
						block-size="20"
					/>
					<text class="progress-text">{{ progressValue }}%</text>
				</view>
				
				<view class="form-item">
					<text class="form-label">进度说明</text>
					<textarea 
						class="progress-desc" 
						placeholder="请详细描述当前进度和完成情况..."
						v-model="progressDesc"
						maxlength="500"
					></textarea>
					<view class="char-count">{{ progressDesc.length }}/500</view>
				</view>
				
				<view class="form-item">
					<text class="form-label">遇到的问题（可选）</text>
					<textarea 
						class="progress-issues" 
						placeholder="如有问题或需要客户配合的地方，请在此说明..."
						v-model="progressIssues"
						maxlength="300"
					></textarea>
					<view class="char-count">{{ progressIssues.length }}/300</view>
				</view>
				
				<view class="form-item">
					<text class="form-label">预计完成时间</text>
					<picker 
						mode="date" 
						:value="estimatedDate"
						@change="onDateChange"
					>
						<view class="date-picker">
							<text>{{ estimatedDate || '请选择日期' }}</text>
							<text class="arrow">📅</text>
						</view>
					</picker>
				</view>
			</view>
		</view>
		
		<!-- 历史进度 -->
		<view class="history-section">
			<view class="section-title">历史进度</view>
			<view class="progress-timeline">
				<view class="timeline-item" v-for="(item, index) in progressHistory" :key="index">
					<view class="timeline-dot"></view>
					<view class="timeline-content">
						<view class="timeline-header">
							<text class="progress-percent">{{ item.progress }}%</text>
							<text class="update-time">{{ formatTime(item.updateTime) }}</text>
						</view>
						<text class="progress-description">{{ item.description }}</text>
						<text class="progress-issues" v-if="item.issues">问题：{{ item.issues }}</text>
					</view>
				</view>
				
				<view class="empty" v-if="progressHistory.length === 0">
					<text>暂无进度记录</text>
				</view>
			</view>
		</view>
		
		<!-- 操作按钮 -->
		<view class="action-section">
			<button 
				class="update-btn" 
				:disabled="!canUpdate || loading"
				@click="updateProgress"
			>
				{{ loading ? '更新中...' : '更新进度' }}
			</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				orderId: '',
				orderInfo: null,
				progressValue: 0,
				progressDesc: '',
				progressIssues: '',
				estimatedDate: '',
				progressHistory: [],
				loading: false
			}
		},
		computed: {
			canUpdate() {
				return this.progressDesc.trim().length > 0
			}
		},
		onLoad(options) {
			this.checkPermission()
			this.orderId = options.orderId
			this.loadOrderInfo()
			this.loadProgressHistory()
		},
		methods: {
			checkPermission() {
				if (!this.$store || !this.$store.getters || !this.$store.getters.isTech) {
					uni.showToast({
						title: '权限不足',
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)
				}
			},
			
			async loadOrderInfo() {
				try {
					const res = await this.$api.getOrderDetail(this.orderId)
					if (res.code === 200) {
						this.orderInfo = res.data
						// 设置当前进度
						this.progressValue = res.data.progress || 0
					}
				} catch (error) {
					uni.showToast({
						title: error.message || '加载订单信息失败',
						icon: 'none'
					})
				}
			},
			
			async loadProgressHistory() {
				try {
					const res = await this.$api.getOrderProgress(this.orderId)
					if (res.code === 200) {
						this.progressHistory = res.data || []
					}
				} catch (error) {
					console.error('加载进度历史失败:', error)
					// 模拟数据
					this.progressHistory = [
						{
							progress: 30,
							description: '已完成需求分析和技术方案设计',
							updateTime: '2024-01-20 10:30:00'
						},
						{
							progress: 60,
							description: '核心功能开发完成，正在进行测试',
							issues: '需要客户提供测试数据',
							updateTime: '2024-01-22 16:45:00'
						}
					]
				}
			},
			
			onProgressChange(e) {
				this.progressValue = e.detail.value
			},
			
			onDateChange(e) {
				this.estimatedDate = e.detail.value
			},
			
			async updateProgress() {
				if (!this.canUpdate) {
					uni.showToast({
						title: '请填写进度说明',
						icon: 'none'
					})
					return
				}
				
				this.loading = true
				
				try {
					const progressData = {
						progress: this.progressValue,
						description: this.progressDesc.trim()
					}
					
					if (this.progressIssues.trim()) {
						progressData.issues = this.progressIssues.trim()
					}
					
					if (this.estimatedDate) {
						progressData.estimatedDate = this.estimatedDate
					}
					
					await this.$api.updateOrderProgress(this.orderId, progressData)
					
					uni.showToast({
						title: '进度更新成功',
						icon: 'success'
					})
					
					// 清空表单
					this.progressDesc = ''
					this.progressIssues = ''
					this.estimatedDate = ''
					
					// 重新加载进度历史
					this.loadProgressHistory()
					
				} catch (error) {
					uni.showToast({
						title: error.message || '更新失败',
						icon: 'none'
					})
				} finally {
					this.loading = false
				}
			},
			
			formatTime(time) {
				if (!time) return ''
				const date = new Date(time)
				return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`
			}
		}
	}
</script>

<style lang="scss" scoped>
	.progress-page {
		background: #f5f5f5;
		min-height: 100vh;
		padding: 20rpx;
		padding-bottom: 120rpx;
	}
	
	.order-card, .progress-section, .history-section {
		background: #fff;
		border-radius: 15rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
	}
	
	.card-title, .section-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 30rpx;
		padding-bottom: 20rpx;
		border-bottom: 2rpx solid #f0f0f0;
	}
	
	.order-info {
		.order-title {
			display: block;
			font-size: 32rpx;
			font-weight: bold;
			color: #333;
			margin-bottom: 20rpx;
		}
		
		.info-row {
			display: flex;
			margin-bottom: 15rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.label {
				font-size: 28rpx;
				color: #666;
				width: 120rpx;
			}
			
			.value {
				flex: 1;
				font-size: 28rpx;
				color: #333;
				
				&.budget {
					font-weight: bold;
					color: #FF6B6B;
				}
			}
		}
	}
	
	.progress-form {
		.form-item {
			margin-bottom: 40rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.form-label {
				display: block;
				font-size: 28rpx;
				font-weight: bold;
				color: #333;
				margin-bottom: 20rpx;
			}
			
			.progress-text {
				text-align: center;
				font-size: 32rpx;
				font-weight: bold;
				color: #007AFF;
				margin-top: 20rpx;
			}
			
			.progress-desc, .progress-issues {
				width: 100%;
				min-height: 200rpx;
				border: 2rpx solid #e0e0e0;
				border-radius: 10rpx;
				padding: 20rpx;
				font-size: 28rpx;
				box-sizing: border-box;
				line-height: 1.5;
				
				&:focus {
					border-color: #007AFF;
				}
			}
			
			.progress-issues {
				min-height: 150rpx;
			}
			
			.char-count {
				text-align: right;
				font-size: 24rpx;
				color: #999;
				margin-top: 10rpx;
			}
			
			.date-picker {
				border: 2rpx solid #e0e0e0;
				border-radius: 10rpx;
				padding: 20rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				font-size: 28rpx;
				
				.arrow {
					font-size: 24rpx;
				}
			}
		}
	}
	
	.progress-timeline {
		.timeline-item {
			display: flex;
			margin-bottom: 40rpx;
			position: relative;
			
			&:last-child {
				margin-bottom: 0;
				
				&::after {
					display: none;
				}
			}
			
			&::after {
				content: '';
				position: absolute;
				left: 15rpx;
				top: 60rpx;
				width: 2rpx;
				height: calc(100% + 20rpx);
				background: #e0e0e0;
			}
			
			.timeline-dot {
				width: 30rpx;
				height: 30rpx;
				border-radius: 15rpx;
				background: #007AFF;
				margin-right: 30rpx;
				margin-top: 10rpx;
				flex-shrink: 0;
			}
			
			.timeline-content {
				flex: 1;
				
				.timeline-header {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-bottom: 15rpx;
					
					.progress-percent {
						font-size: 28rpx;
						font-weight: bold;
						color: #007AFF;
					}
					
					.update-time {
						font-size: 24rpx;
						color: #999;
					}
				}
				
				.progress-description {
					display: block;
					font-size: 28rpx;
					color: #333;
					line-height: 1.5;
					margin-bottom: 10rpx;
				}
				
				.progress-issues {
					display: block;
					font-size: 24rpx;
					color: #ff9500;
					background: rgba(255, 149, 0, 0.1);
					padding: 10rpx 15rpx;
					border-radius: 8rpx;
					line-height: 1.4;
				}
			}
		}
		
		.empty {
			text-align: center;
			padding: 80rpx 0;
			color: #999;
			font-size: 28rpx;
		}
	}
	
	.action-section {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background: #fff;
		padding: 20rpx;
		border-top: 1rpx solid #eee;
		
		.update-btn {
			width: 100%;
			height: 88rpx;
			background: linear-gradient(45deg, #007AFF, #0056CC);
			color: #fff;
			border: none;
			border-radius: 44rpx;
			font-size: 32rpx;
			font-weight: bold;
			
			&:disabled {
				opacity: 0.6;
				background: #ccc;
			}
		}
	}
</style>